﻿using MongoDB.Bson.Serialization.Attributes;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace nMongo.Tests.Free
{
    class Test1
    {
        [BsonId]
        public Guid Id { get; set; }

        [BsonElement]
        protected Guid _foreignId;
        [BsonIgnore]
        protected Test2 _foreignObject;
        [BsonIgnore]
        public Test2 ForeignObject
        {
            get { return _foreignObject; }
            set
            {
                _foreignObject = value;
                _foreignId = value.Id;
            }
        }
    }

    class Test2
    {
        [BsonId]
        public Guid Id { get; set; }

        public string AnyValue { get; set; }
    }

    class Test3
    {
        public Test3()
        {
            _foreignId = new List<Guid>();
            _foreignObjects = new ObservableCollection<Test2>();
            ForeignObjects.CollectionChanged += ForeignObjects_CollectionChanged;
        }

        [BsonId]
        public Guid Id { get; set; }

        [BsonElement]
        protected List<Guid> _foreignId;
        [BsonIgnore]
        protected ObservableCollection<Test2> _foreignObjects;
        [BsonIgnore]
        public ObservableCollection<Test2> ForeignObjects
        {
            get { return _foreignObjects; }
            set
            {
                _foreignObjects = null;
                _foreignObjects = value;
            }
        }

        protected void ForeignObjects_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            _foreignId.Clear();
            _foreignObjects.ToList().ForEach(x => _foreignId.Add(x.Id));            
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            MongoMapset.MapAssociation().OneToMany<Type, ArgumentException>(x => x.Name, y => y.Message);
            return;

            MongoEngine engine = new MongoEngine("");
            Test2 test2_a = new Test2 { Id = Guid.NewGuid(), AnyValue = System.IO.Path.GetRandomFileName() };
            Test2 test2_b = new Test2 { Id = Guid.NewGuid(), AnyValue = System.IO.Path.GetRandomFileName() };
            Test2 test2_c = new Test2 { Id = Guid.NewGuid(), AnyValue = System.IO.Path.GetRandomFileName() };
            Test2 test2_d = new Test2 { Id = Guid.NewGuid(), AnyValue = System.IO.Path.GetRandomFileName() };
            engine.Save<Test2>(test2_a);
            engine.Save<Test2>(test2_b);
            engine.Save<Test2>(test2_c);
            engine.Save<Test2>(test2_d);

            //Test1 test1_a = new Test1
            //{
            //    Id = Guid.NewGuid(),
            //    ForeignObject = test2_a
            //};
            //engine.Save<Test1>(test1_a);

            //Test1 test1_a_returned = engine.Select<Test1>(x => x.Id == test1_a.Id).Include<Test2>("_foreignId", "ForeignObject").FirstOrDefault();

            Test3 test3 = new Test3 { Id = Guid.NewGuid() };
            test3.ForeignObjects.Add(test2_a);
            test3.ForeignObjects.Add(test2_b);
            test3.ForeignObjects.Add(test2_c);
            test3.ForeignObjects.Add(test2_d);
            engine.Save<Test3>(test3);
            Test3 test3_returned = engine.Select<Test3>().Include<Test2>("_foreignId", "ForeignObjects").FirstOrDefault();
        }
    }
}
